package com.chapter.sample.jvm.load;

import org.junit.Test;

import java.io.*;
import java.util.Random;


/**
 * 测试类的主动使用: 意味着会调用类的<clinit>(),即执行了类的初始化
 * <p>
 * 1、当创建一个类的实例时，比如使用new关键字，或者通过反射、克隆、序列化.
 * 2、当调用类的静态方法时，即当使用了字节码invockestatic指令
 */
public class ActiveUse1 {
    public static void main(String[] args) {

        Order order = new Order();
    }

    /**
     * 序列化
     */
    @Test
    public void test1() {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("order.dat"));
            oos.writeObject(new Order());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 反序列化
     */
    @Test
    public void test2() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("order.dat"));
            Order order = (Order) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Test
    public void test3() {
        Order.method();
    }

    @Test
    public void test4() {
        System.out.println(Order.i);
    }
}


class Order implements Serializable, MyInterface {

    //static int i = 10
    //static final int i = 10;//在链接环节的准备阶段赋值
    static final int i = new Random().nextInt(10);//初始化阶段赋值 <clinit>

    static {
        System.out.println("Order 类的初始化");
    }

    public static void method() {
        System.out.println("Order method() ......");
    }
}

interface MyInterface {
    public static final Thread t = new Thread() {
        {
            System.out.println("MyInterface的初始化");
        }
    };

    public default void method1() {
        System.out.println("default 方法");
    }
}

